package com.androidcare;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class AccountList extends ListActivity {
	protected AccountManager accountManager;
	protected Intent intent;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        accountManager = AccountManager.get(getApplicationContext());
        Account[] accounts = accountManager.getAccountsByType("com.google");
        this.setListAdapter(new ArrayAdapter<Account>(this, R.layout.list_item, accounts));        
    }

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		Account account = (Account)getListView().getItemAtPosition(position);

		String filename="MyFile.txt";
		String accountname=account.name;
		FileOutputStream fOu;
		try {
			fOu = getBaseContext().openFileOutput(filename, MODE_WORLD_WRITEABLE);
			OutputStreamWriter osw = new OutputStreamWriter(fOu); 
			osw.write(accountname);
			osw.close();
			fOu.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			Log.i("AccountList","Fichero Cuentas no encontrado");
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			Log.i("AccountList","Error IO");
			e.printStackTrace();
		}
		Intent intent = new Intent(getApplicationContext(),AppInfo.class);
		intent.putExtra("account", account);
		startActivity(intent);
		finish();	
	}
}